'''
    TV user's methods
'''
import MySQLdb
import types
from WeiboUser import WeiboUser
from conf import *
from MySqlDBHandle import MySqlDBHandle

class TvUser(WeiboUser):
    
    def __init__(self, iUid):
        self.mUid = iUid
        WeiboUser.__init__(self, iUid, 'LU_Tv', 'TV_Follower_Relationship')
        
    def  process(self):
        self._WeiboUserAPIProcess()
        if type(self.mJsonData) == types.DictType and not self.mJsonData.has_key('error'):
            self._InsertTvToDB()
        self._InsertFollowersToDB()

    def _IsUserIdInDB(self):
        lSQLStatement = "SELECT * FROM LU_TV WHERE weiboId = %s;" % self.mJsonData['id']
        result = MySqlDBHandle.execSqlStatement(lSQLStatement)
        if result is None:
            return False
        else:
            return True
    
    def _InsertTvToDB(self):
        if self._IsUserIdInDB():
            return
        else:
            try:
                lSQLStatement = "INSERT INTO LU_TV (name, weiboId, LAST_UPDATE_TIMESTAMP) VALUES ('%s', %s, current_timestamp);" % (self.mJsonData['screen_name'], self.mJsonData['id'])
                MySqlDBHandle.execSqlStatement(lSQLStatement)
            except Exception, e:
                #Tdd some process if necessary
                print "Error in TvUser.py InsertTvToDB function"
    
    # Insert the followers of TV, and also insert the relationships into tv_follower_realtionship
            